package com.chocho.producerserver.service;

import com.chocho.producerserver.bean.QCollection;
import com.chocho.producerserver.bean.Question;
import com.chocho.producerserver.dao.QCollectionDao;
import com.chocho.producerserver.dao.QuestionDao;
import com.chocho.producerserver.utils.JwtUtil;
import com.chocho.producerserver.utils.UUIDUtil;
import com.chocho.producerserver.vo.VCollection;
import com.chocho.producerserver.vo.VQuestion;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.sql.Date;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by chocho on 2022/4/10.
 */
@Service
public class QCollectionService {
    @Autowired
    private QCollectionDao collectionDao;

    @Autowired
    private QuestionDao questionDao;

    public boolean addCollection(HttpServletRequest request, VCollection c){
        QCollection collection = new QCollection();
        collection.setQcollectionId(UUIDUtil.getUUID());
        collection.setQcollectionName(c.getName());

        String token = request.getHeader("token");
        try {
            Claims claims = JwtUtil.parseJWT(token);
            collection.setUserId(claims.getSubject());
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("token非法");
        }

        collection.setCreateTime(new Date(System.currentTimeMillis()));
        collection.setEndTime(c.getEndTime());
        collection.setQcollectionStatus(1);

        return collectionDao.insert(collection) > 0;
    }

    public List<VCollection> getCollectionByQuery(Map<String, Object> map){
        List<QCollection> list = collectionDao.getCollectionByQuery(map);
        List<VCollection> collectionList = new ArrayList<>();
        for(QCollection c : list){
            VCollection v = new VCollection();
            v.setId(c.getQcollectionId());
            v.setName(c.getQcollectionName());
            //TODO 后面需要替换
            v.setAuthor("chocho");
            v.setCreateTime(c.getCreateTime());
            v.setEndTime(c.getEndTime());
            v.setStatus(c.getQcollectionStatus());

            collectionList.add(v);
        }

        return collectionList;
    }

    public List<VCollection> getMyCollectionByQuery(HttpServletRequest request, Map<String, Object> map){
        String token = request.getHeader("token");
        try {
            Claims claims = JwtUtil.parseJWT(token);
            map.put("userId", claims.getSubject());
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("token非法");
        }
        List<QCollection> list = collectionDao.getCollectionByQuery(map);
        List<VCollection> collectionList = new ArrayList<>();
        for(QCollection c : list){
            VCollection v = new VCollection();
            v.setId(c.getQcollectionId());
            v.setName(c.getQcollectionName());
            //TODO 后面需要替换
            v.setAuthor("chocho");
            v.setCreateTime(c.getCreateTime());
            v.setEndTime(c.getEndTime());
            v.setStatus(c.getQcollectionStatus());

            collectionList.add(v);
        }

        return collectionList;
    }

    public boolean deleteCollection(String ids){
        List<String> idList = new ArrayList<>();
        String []idArr = ids.split("&");
        for(int i = 0; i < idArr.length; i++){
            idList.add(idArr[i]);
        }

        return collectionDao.delete(idList) > 0;
    }

    public boolean updateCollection(HttpServletRequest request, VCollection v){
        QCollection collection = new QCollection();
        collection.setQcollectionId(v.getId());
        collection.setQcollectionName(v.getName());

        String token = request.getHeader("token");
        try {
            Claims claims = JwtUtil.parseJWT(token);
            collection.setUserId(claims.getSubject());
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("token非法");
        }

        collection.setCreateTime(v.getCreateTime());
        collection.setEndTime(v.getEndTime());
        collection.setQcollectionStatus(v.getStatus());

        return collectionDao.update(collection) > 0;
    }

    public List<VCollection> getCollectionByIdList(String ids){
        List<String> idList = new ArrayList<>();
        String []idArr = ids.split("&");
        for(int i = 0; i < idArr.length; i++){
            idList.add(idArr[i]);
        }
        List<QCollection> list = collectionDao.getByIdList(idList);
        List<VCollection> collectionList = new ArrayList<>();
        for(QCollection c : list){
            VCollection v = new VCollection();
            v.setId(c.getQcollectionId());
            v.setName(c.getQcollectionName());
            //TODO 后面需要替换
            v.setAuthor("chocho");
            v.setCreateTime(c.getCreateTime());
            v.setEndTime(c.getEndTime());
            v.setStatus(c.getQcollectionStatus());

            collectionList.add(v);
        }

        return collectionList;

    }

    public int getAllScore(String id){
        Integer count = questionDao.getAllScoreById(id);
        return (count != null) ? count : 0;
    }

    public int count(){
        return collectionDao.getMaxNum();
    }

    public List<Integer> getDoNum(String ids){
        List<String> idList = new ArrayList<>();
        String []idArr = ids.split("&");
        for(int i = 0; i < idArr.length; i++){
            idList.add(idArr[i]);
        }

        return questionDao.selectByIdList(idList);
    }
}
